﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using QuanLyGiaoVuService.CommonServices;
using QuanLyGiaoVuService.ReportServices;
using QuanLyGiaoVuService.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.CommandServices
{
    public class LopMonHocCommandService : ILopMonHocCommandService
    {
        public ILopMonHocRepository _lopMonHocRepository;
        public IMonHocRepository _monHocRepository;
        public IDangKyGioDayCommandService _dangKyGioDayCommandService;
        public IDangKyGioDayReportService _dangKyGioDayReportService;
        public ITietHocService _tietHocService;

        public LopMonHocCommandService(ILopMonHocRepository lopMonHocRepository, IDangKyGioDayCommandService dangKyGioCommandService, IDangKyGioDayReportService dangKyGioReportService, ITietHocService tietHocService, IMonHocRepository monHocRepository)
        {
            _lopMonHocRepository = lopMonHocRepository;
            _dangKyGioDayCommandService = dangKyGioCommandService;
            _dangKyGioDayReportService = dangKyGioReportService;
            _tietHocService = tietHocService;
            _monHocRepository = monHocRepository;
        }

        public QuanLyGiaoVuDAL.DTO.LopMonHocDTO InsertLopMonHoc(QuanLyGiaoVuDAL.DTO.LopMonHocDTO lopMonHocDTO)
        {
            lopMonHocDTO.LopMonHocId = Guid.NewGuid();

            if (lopMonHocDTO.LoaiLop != ThamSoConstantUtility.LOAILOPLYTHUYET) //Lop Thuc Hanh
            {
                lopMonHocDTO.DaPhanHoi = true;
                lopMonHocDTO.DaXet = true;
                lopMonHocDTO.DongYMoLop = true;
                lopMonHocDTO.GiangVienXacNhan = true;
            }
            return _lopMonHocRepository.InsertLopMonHoc(lopMonHocDTO);
        }

        public QuanLyGiaoVuDAL.DTO.LopMonHocDTO UpdateLopMonHoc(QuanLyGiaoVuDAL.DTO.LopMonHocDTO lopMonHocDTO)
        {
            if (lopMonHocDTO.LoaiLop != ThamSoConstantUtility.LOAILOPLYTHUYET)
            {
                lopMonHocDTO.DaPhanHoi = true;
                lopMonHocDTO.DaXet = true;
                lopMonHocDTO.DongYMoLop = true;
                lopMonHocDTO.GiangVienXacNhan = true;
            }
            return _lopMonHocRepository.UpdateLopMonHoc(lopMonHocDTO);
        }

        public bool DeleteLopMonHocById(Guid lopMonHocId)
        {
            return _lopMonHocRepository.DeleteLopMonHoc(lopMonHocId);
        }


        public QuanLyGiaoVuDAL.DTO.LopMonHocDTO GiangVienXacNhan(Guid lopMonHocId, bool giangVienXacNhan)
        {
            var lopMonHoc = _lopMonHocRepository.GetLopMonHocById(lopMonHocId);
            lopMonHoc.GiangVienXacNhan = giangVienXacNhan;
            lopMonHoc.DaPhanHoi = true;
            return _lopMonHocRepository.UpdateLopMonHoc(lopMonHoc);
        }


        public void XacNhanDangKy(Guid maThongTinDangKyGioDay)
        {
            DangKyGioDayDTO thongTinDangKy = new DangKyGioDayDTO();
            thongTinDangKy = _dangKyGioDayReportService.GetThongTinDangKyGioDayById(maThongTinDangKyGioDay);

            LopMonHocDTO lopMonHoc = _lopMonHocRepository.GetLopMonHocById(thongTinDangKy.MaLopMonHoc);
            lopMonHoc.MaTietDau = thongTinDangKy.MaTietDau;
            lopMonHoc.MaTietSau = thongTinDangKy.MaTietSau;

            lopMonHoc.Thu = thongTinDangKy.Thu;
            _lopMonHocRepository.UpdateLopMonHoc(lopMonHoc);

            var danhSachDK = _dangKyGioDayReportService.GetAllThongTinDangKyGioDay(null, thongTinDangKy.MaLopMonHoc, null, null, null, null);
            foreach (var dangKy in danhSachDK)
            {
                dangKy.DuocChon = false;
                _dangKyGioDayCommandService.UpdateThongTinDangKyGioDay(dangKy);
            }

            var dangKyDuocChon = _dangKyGioDayReportService.GetThongTinDangKyGioDayById(maThongTinDangKyGioDay);
            dangKyDuocChon.DuocChon = true;
            _dangKyGioDayCommandService.UpdateThongTinDangKyGioDay(dangKyDuocChon);
        }


        public LopMonHocDTO XepLich(LopMonHocDTO lopMonHoc, int khaNang)
        {
            lopMonHoc.Thu = (short)((khaNang - 1) / 4 + 2);
            TietHocDTO tietDau, tietSau;

            if (lopMonHoc.MonHoc.SoTinChiLyThuyet <= 2)
            {
                int ca = (khaNang % 4);
                if (ca == 0)
                    ca = 4;
                _tietHocService.LayTietHocTheoCa(ca, out tietDau, out tietSau);
            }
            else
            {
                bool buoiSang = (khaNang % 4) == 1 || (khaNang % 4) == 2;
                _tietHocService.LayTietHocTheoBuoi(!buoiSang, out tietDau, out tietSau);
            }
            lopMonHoc.MaTietDau = tietDau.MaTiet;
            lopMonHoc.TietDau = tietDau;
            lopMonHoc.MaTietSau = tietSau.MaTiet;
            lopMonHoc.TietSau = tietSau;
            return lopMonHoc;
        }
    }
}
